/**  
 * @Title: PrivilegeTag.java
 * @Package com.xzit.uscdl.common.tag
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年3月15日 下午5:39:51
 * @version V1.0  
 */
package com.xzit.tmms.common.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.xzit.tmms.common.constant.WebConstant;
import com.xzit.tmms.common.util.CommonUtil;
import com.xzit.tmms.common.util.privilege.PrivilegeUtil;

/**
 * @ClassName: PrivilegeTag
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年3月15日 下午5:39:51
 */
public class PrivilegeTag extends BodyTagSupport {

	/**
	 * @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 2159929111806258075L;

	private String action;

	@SuppressWarnings("rawtypes")
	@Override
	public int doStartTag() throws JspException {
		if (CommonUtil.isNotEmpty(action)) {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			HttpSession session = request.getSession();
			// 管理员登录
			Map user = (Map) session.getAttribute(WebConstant.SESSION_ADMIN);
			// 前台用户
			if (user == null) {
				user = (Map) session.getAttribute(WebConstant.SESSION_USER);
			}
			if (user != null) {
				// 公共资源
				if(PrivilegeUtil.isPublic(action)){
					return EVAL_BODY_INCLUDE;
				}
				// 超级管理员
				if (user.get("superAdmin") != null && user.get("superAdmin").toString().equals("1")) {
					return EVAL_BODY_INCLUDE;
				} else {
					// 权限校验
					long[] rights = (long[]) user.get("rights");
					if (PrivilegeUtil.hasRight(action, rights)) {
						return EVAL_BODY_INCLUDE;
					}
				}
			}
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * @return the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * @param action
	 *            the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}

	@Override
	public void release() {
		action = null;
		super.release();
	}

}
